package com.ssm.controller;

import java.io.FileInputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.Page;
import com.ssm.model.AssessPlanModel;
import com.ssm.model.LoginInfo;
import com.ssm.model.ProfessorModel;
import com.ssm.model.TeacherModel;
import com.ssm.service.MaterialService;
import com.ssm.service.ProfessorService;
import com.ssm.util.SftpUtil;

@Controller
@RequestMapping("/professor")
public class ProfessorController {

	//头像地址
	private static final String HEAD_URL_PATH = "http://120.79.86.68:8080/upload/headUrl/";

	@Autowired
	private ProfessorService professorService;
	@Autowired
	private MaterialService materialService;

	//professor首页
	/**
	 * professor首页
	 * @param request 获得shiro subject实体中已登录用户信息
	 * @param model 返回页面显示信息-用户头像-用户名-用户信息完整度
	 * @return 返回专家首页
	 */
	@RequestMapping("/index.do")
	public String showIndex(Model model,HttpServletRequest request){
		Subject subject = SecurityUtils.getSubject();
		LoginInfo loginInfo = (LoginInfo)subject.getPrincipal();
		Integer pId = loginInfo.getLoginId();
		ProfessorModel professorModel = professorService.selectById(pId);
		request.getSession().setAttribute("sessionPhead", professorModel.getpHeadUrl());
		request.getSession().setAttribute("sessionPid", pId);
		request.getSession().setAttribute("sessionPinfo", professorModel.getpInfo());
		model.addAttribute("professor",professorModel);
		return "professor/index";
	}
	//个人信息设置
	/**
	 *  跳转到个人信息设置页面
	 * @param request 获取session中用户Id
	 * @param model 绑定专家信息
	 * @return 返回个人信息页面
	 */
	@RequestMapping("/info.do")
	public String info(Model model,HttpServletRequest request){
		Integer pId = (Integer)request.getSession().getAttribute("sessionPid");
		ProfessorModel professorModel = professorService.selectById(pId);
		model.addAttribute("professor", professorModel);
		return "professor/info";
	}
	@InitBinder
	public void initBinder(WebDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));   //true:允许输入空值，false:不能为空值
	}
	//更新个人信息
	/**
	 * 更新个人信息
	 * @param imgFile 头像文件-通过sftp服务上传至服务器，获得返回文件地址
	 * @param professorModel 其余专家信息
	 * @return 重定向至首页
	 * @throws Exception
	 */
	@RequestMapping(value = "/completeInfo.do")
	public String saveInfo(ProfessorModel professorModel,MultipartFile imgFile) throws Exception{
		if(!imgFile.isEmpty()){
			//操作用户id
			Subject subject = SecurityUtils.getSubject();
			Integer pId = professorModel.getpId();
			//文件名
			String fileName = imgFile.getOriginalFilename();
			//文件地址
			String headUrl = HEAD_URL_PATH + pId + "/" + fileName;
			//文件类型：头像
			Integer uploadType = 1;
			//上传源文件inputStream
			FileInputStream inputStream = (FileInputStream)imgFile.getInputStream();
			//使用sftp工具上传文件至服务器
			SftpUtil.uploadToService(inputStream, fileName, pId.toString(), uploadType);
			System.out.println("头像上传成功!");
			professorModel.setpHeadUrl(headUrl);
		}
		//上传完成后将用户信息更新到数据库
		//完善个人信息，tInfo==1
		professorModel.setpInfo(1);
		professorService.updateInfo(professorModel);
		return "redirect:index.do";
	}
	//安全中心
	/**
	 *  显示安全中心
	 * @return 返回显示页面
	 */
	@RequestMapping("/security.do")
	public String security(){
		return "professor/security";
	}
	//安全中心-重置密码
	@RequestMapping("/resetPwd.do")
	public String resetPwd(Model model){
		Subject subject = SecurityUtils.getSubject();
		LoginInfo loginInfo = (LoginInfo)subject.getPrincipal();
		model.addAttribute("loginId",loginInfo.getLoginId());
		return "professor/resetPwd";
	}
	//安全中心-重置邮箱

	//评估管理
	/**
	 *  评估管理-教师材料文件-教室列表
	 * @param pageNum 分页页码
	 * @param request 专家Id查询专家所负责的教师列表
	 * @param model 封装当前专家所负责的教师列表
	 * @return 返回列表显示
	 */
	@RequestMapping("/assessMaterial.do")
	public String assessControl1(Integer pageNum,Model model,HttpServletRequest request){
		Integer pId = (Integer)request.getSession().getAttribute("sessionPid");
		if(pageNum==null){
			pageNum = 1;
		}
		Page<TeacherModel> teacherModels = professorService.selectTeacherByPid(pId, pageNum);
		model.addAttribute("pageInfo",teacherModels.toPageInfo());
		return "professor/checkMaterial";
	}

	/**
	 * 获取教师材料文件
	 * @param tId 教师Id
	 * @param model 数据：材料列表及文件数据
	 * @return 返回显示页面
	 */
	@RequestMapping("/getMaterialDetail.do")
	public String getMaterialDetail(Integer tId,Model model){
		List materialList = materialService.selectTeacherMaterial(tId);
		model.addAttribute("dataList",materialList);
		return "professor/materialDetail";
	}

	/**
	 * 获取教师总结
	 * @param tId 教师id
	 * @param model 数据：总结列表及内容
	 * @return 返回页面显示
	 */
	@RequestMapping("/getSummaryDetail.do")
	public String getSummaryDetail(Integer tId,Model model){
		List summaryList = materialService.selecTeacherSummaryByTid(tId);
		model.addAttribute("dataList", summaryList);
		return "professor/summaryDetail";
	}

	//下载文件
	/**
	 * 教师材料文件下载
	 * @param filePath 文件在服务器上的地址
	 * @return 返回操作结果信息以及文件下载后的地址 json
	 */
	@ResponseBody
	@RequestMapping("/downloadFile.do")
	public Object downloadFile(String filePath){
		System.out.println("文件下载地址："+filePath);
		//使用sftp工具类从服务器上下载文件至本地
		String downPath = SftpUtil.downloadFile(filePath);
		HashMap map = new HashMap<>();
		map.put("success", true);
		map.put("downPath", downPath);
		return map;
	}

	//评估管理-教师总结
	/**
	 * 评估管理-专家总结-教师列表
	 * @param pageNum 分页页码
	 * @param request 获取当前专家Id
	 * @param model 封装当前专家所负责的教师信息
	 * @return 返回页面显示
	 */
	@RequestMapping("/assessSummary.do")
	public String assessSummary(Integer pageNum,Model model,HttpServletRequest request){
		Integer pId = (Integer)request.getSession().getAttribute("sessionPid");
		if(pageNum==null){
			pageNum = 1;
		}
		Page<TeacherModel> teacherModels = professorService.selectTeacherByPid(pId, pageNum);
		model.addAttribute("pageInfo",teacherModels.toPageInfo());
		return "professor/checkSummary";
	}
	//评估管理-评估进度
	/**
	 * 评估管理-评估进度-教师列表
	 * @param pageNum 分页页码
	 * @param request 专家Id
	 * @param model 数据： 专家所负责的教师列表
	 * @return 返回页面显示
	 */
	@RequestMapping("/assessSchedule.do")
	public String assessSchedule(Integer pageNum,Model model,HttpServletRequest request){
		Integer pId = (Integer)request.getSession().getAttribute("sessionPid");
		if(pageNum==null){
			pageNum = 1;
		}
		Page<TeacherModel> teacherModels = professorService.selectTeacherByPid(pId, pageNum);
		model.addAttribute("pageInfo",teacherModels.toPageInfo());
		return "professor/assessSchedule";
	}

	/**
	 * ajax异步加载指定教师的评估进度
	 * @param tId 指定教师的Id
	 * @return 返回指定教师的评估进度信息 json
	 */
	@ResponseBody
	@RequestMapping("/getScheduleByTid.do")
	public Object getScheduleByTid(Integer tId){
		AssessPlanModel assessPlanModel = professorService.selectAssessPlanByTid(tId);
		HashMap map = new HashMap<>();
		map.put("plan", assessPlanModel);
		return map;
	}

}
